<template>
	<view>
		<view class="uni-header">
			<view class="uni-group">
				<view class="uni-title"></view>
				<view class="uni-sub-title"></view>
			</view>
			<view class="uni-group">
				<input class="uni-search" type="text" v-model="query" placeholder="请输入搜索内容" />
				<button class="uni-button" type="default" size="mini" @click="search">搜索</button>
			</view>
		</view>
		<view class="uni-container">
			<unicloud-db ref="udb" :collection="collectionList" :options="options" :where="where" :field="field"
				page-data="replace" :orderby="orderby" :getcount="true" :page-size="options.pageSize"
				:page-current="options.pageCurrent" v-slot:default="{data,pagination,loading,error}">
				<uni-table :loading="loading" :emptyText="error.message || '没有更多数据'" border stripe>
					<uni-tr>
						<uni-th align="center">ID号</uni-th>
						<uni-th align="center">邀请码</uni-th>
						<uni-th align="center">昵称</uni-th>
						<uni-th align="center">头像</uni-th>
						<uni-th align="center">手机号</uni-th>
						<uni-th align="center">账号状态</uni-th>
						<uni-th align="center">金币</uni-th>
						<uni-th align="center">身份证号</uni-th>
						<uni-th align="center">真实姓名</uni-th>
						<uni-th align="center">支付宝账号</uni-th>
						<uni-th align="center">直推好友人数</uni-th>
						<uni-th align="center">扩散好友人数</uni-th>
						<uni-th align="center">猫等级</uni-th>
						<uni-th align="center">每秒产金币数量</uni-th>
						<uni-th align="center">可提现金额</uni-th>
						<uni-th align="center">好友累计赚钱</uni-th>
						<uni-th align="center">旅行团阶段</uni-th>
						<uni-th align="center">看视频次数</uni-th>
						<uni-th align="center">必得分红猫进度</uni-th>
						<uni-th width="170" align="center">注册时间</uni-th>
						<uni-th width="204" align="center">操作</uni-th>
					</uni-tr>
					<uni-tr v-for="(item,index) in data" :key="index">
						<uni-td align="center">{{item._id[0]._id}}</uni-td>
						<uni-td align="center">{{item.inviteCode}}</uni-td>
						<uni-td align="center">{{item.nickname}}</uni-td>
						<uni-td align="center">
							<image :src="item.avatar" mode="" style="width: 80rpx;height: 80rpx;"></image>
						</uni-td>
						<uni-td align="center">{{item.phone}}</uni-td>
						<uni-td align="center" v-if="item._id[0]&&item._id[0].status">
							{{ item._id[0].status == 4 ? '账号已注销' : '正常' }}</uni-td>
						<uni-td align="center" v-else>{{ item.accountstatus == 1 ? '账号已封禁' : '正常' }}</uni-td>
						<uni-td align="center">{{parseInt(item.coin)}}</uni-td>
						<uni-td align="center">{{item.idcard}}</uni-td>
						<uni-td align="center">{{item.realname}}</uni-td>
						<uni-td align="center">{{item.zhifubao}}</uni-td>
						<uni-td align="center">{{item.zhituifriend}}</uni-td>
						<uni-td align="center">{{item.kuosanfriend}}</uni-td>
						<uni-td align="center">{{item.maxlvl}}</uni-td>
						<uni-td align="center">{{item.persec}}</uni-td>
						<uni-td align="center">{{item.fen?(parseInt(item.fen)/1000):0}}</uni-td>
						<uni-td align="center">{{item.friendfen?(parseInt(item.friendfen)/1000):0}}</uni-td>
						<uni-td align="center">{{item.stage}}</uni-td>
						<uni-td align="center">{{item.videocount}}</uni-td>
						<uni-td align="center">{{item.bide}}</uni-td>
						<uni-td align="center">
							<uni-dateformat :date="item.createtime * 1000" :threshold="[0, 0]" />
						</uni-td>
						<uni-td align="center">
							<view class="uni-group">
								<button  @click="confirmOpenUser(item)" v-if="item._id[0]&&item._id[0].status" class="uni-button" size="mini"
									type="primary">{{item._id[0].status === 4?'恢复已注销账号':'账号正常'}}</button>
								<button v-else @click="confirmDelete(item)" class="uni-button" size="mini"
									type="warn">{{item.accountstatus==0?'封号':'解封'}}</button>
							</view>
						</uni-td>
					</uni-tr>
				</uni-table>
				<view class="uni-pagination-box">
					<uni-pagination show-icon :page-size="pagination.size" v-model="pagination.current"
						:total="pagination.count" @change="onPageChanged" />
				</view>
			</unicloud-db>
		</view>
	</view>
</template>

<script>
	const db = uniCloud.database()
	// 表查询配置
	const dbCollectionName = 'userinfo,uni-id-users'
	const dbOrderBy = 'createtime desc' // 排序字段
	const dbSearchFields = ['_id', "nickname", "phone"] // 支持模糊搜索的字段列表

	const dbField =
		'_id{_id,status},inviteCode,avatar,nickname,phone,coin,idcard,realname,zhifubao,zhituifriend,kuosanfriend,maxlvl,persec,fen,friendfen,stage,videocount,bide,createtime,accountstatus'
	// 分页配置
	const pageSize = 20
	const pageCurrent = 1

	import {
		mapState
	} from 'vuex'
	export default {
		data() {
			return {
				collectionList: [
					db.collection('userinfo').getTemp(),
					db.collection('uni-id-users').getTemp()
				],
				query: '',
				where: '',
				field: dbField,
				orderby: dbOrderBy,
				collectionName: dbCollectionName,
				options: {
					pageSize,
					pageCurrent
				}
			}
		},
		computed: {
			...mapState('user', ['userInfo']),
		},
		methods: {
			getWhere() {
				const query = this.query.trim()
				if (!query) {
					return ''
				}
				const queryRe = new RegExp(query, 'i')
				return dbSearchFields.map(name => queryRe + '.test(' + name + ')').join(' || ')
			},
			search() {
				const newWhere = this.getWhere()
				const isSameWhere = newWhere === this.where
				this.where = newWhere
				if (isSameWhere) { // 相同条件时，手动强制刷新
					this.loadData()
				}
			},
			loadData(clear = true) {
				this.$refs.udb.loadData({
					clear
				})
			},
			onPageChanged(e) {
				this.$refs.udb.loadData({
					current: e.current
				})
			},
			confirmDelete(item) {
				console.log("item: ",item);
				uni.showModal({
					title: '提示',
					content: '确定' + (item.accountstatus?'解封':'封禁') + '此用户吗？',
					showCancel: true,
					cancelText: '取消',
					confirmText: '确定',
					success: res => {
						console.log("res: ", res);
						if (res.confirm) {
							uni.showLoading({
								title: '处理中...',
								mask: false
							});
							this.$request('admin/fenghao', {
								fenghaoid: item._id[0]._id
							})
							.then(res => {
								uni.hideLoading()
								this.loadData()
							}).catch(err => {
								uni.hideLoading()
								console.log("err: ",err);
							})
						}
					},
					fail: () => {},
					complete: () => {}
				});
			},
			confirmOpenUser(item){
				console.log("item: ",item);
				uni.showModal({
					title: '提示',
					content: '此用户已注销账号，确定恢复账号吗？',
					showCancel: true,
					cancelText: '取消',
					confirmText: '确定',
					success: res => {
						console.log("res: ", res);
						if (res.confirm) {
							uni.showLoading({
								title: '处理中...',
								mask: false
							});
							this.$request('admin/openUser', {
								openUserId: item._id[0]._id 
							})
							.then(res => {
								console.log("res: ",res);
								uni.hideLoading()
								this.loadData()
							}).catch(err => {
								uni.hideLoading()
								console.log("err: ",err);
							})
						}
					},
					fail: (err) => {
						console.log("err: ",err);
					},
					complete: (com) => {
						console.log("com: ",com);
					}
				});
			}
		}
	}
</script>
<style>
</style>
